package com.hqyj.system.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.UUID;

/**
 * @author Guo.Yang
 * @version 1.0
 * @date 2021/12/2 13:21
 */
@Controller
@RequestMapping("source")
public class SourceController {
    @GetMapping("code")
    public void code(HttpSession session, HttpServletResponse response) throws IOException {
        BufferedImage bufferedImage = new BufferedImage(150, 40, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.white);
        graphics.fillRect(0,0,150,40);
        graphics.setColor(Color.RED);
        String substring = UUID.randomUUID().toString().replace("-", "").substring(5, 11);
        graphics.setFont(new Font("",Font.BOLD,30));
        graphics.drawString(substring,15,35);
        session.setAttribute("code",substring);
        ImageIO.write(bufferedImage,"JPEG",response.getOutputStream());
    }
}
